import axios from 'axios'
import qs from 'qs'
import router from '../router'
// let url='http://192.168.1.52:8088/api/back/'  //后台本机
let url='http://192.168.1.75:8088/api/back/'  //开发使用
// let url='http://green.jiagj.com/api/back/'  //正式服务

// let url='http://back.jiagj.com/api/back/'  //公测环境

let user=sessionStorage.getItem('user');
let greenToken=''
if(user){
    user= JSON.parse(user)
    greenToken=user.greenToken;
}else{
    router.push({path:'/login'})
}
// Axios.defaults.baseURL = "http://localhost:8080"
// http request 拦截器
axios.interceptors.request.use((config)=>{
    if(config.method=='get' || config.method=='delete'){
        config.headers['Content-Type'] = 'application/x-www-form-urlencoded' //设置拦截器, 把content-type 设置成application/x-www-form-urlencoded, 降级为简单请求, 就不会发送options请求
    }
    return config
  })
axios.interceptors.response.use((response)=>{
    // console.log(response) //响应拦截
    return response;
})

export default axios // 这句千万不能漏下！！！

let request = {
    url: url,
    headerJson: {
        // "contentType": "application/json;charset=utf-8",
        'greenToken': greenToken
    },
    headerFrom:{
        'Content-Type': 'application/x-www-form-urlencoded',
        'greenToken': greenToken
    },
    header:{
        'greenToken': greenToken
    },
    delAlert(obj){ //删除弹框
        if(obj){
            obj.name=obj.name? "此操作将"+obj.name+", 是否继续?" : "此操作是永久删除, 是否继续?"
        }
        obj.that.$confirm(obj.name, "提示", {
            confirmButtonText: "确定",
            cancelButtonText: "取消",
            type: "warning"
        })
        .then(() => {
            let aa={
                type: "success",
                message: "操作成功"
            }
            obj.fn(aa)
        })
        .catch(() => {
            obj.that.$message({
                type: "info",
                message: "已取消操作"
            });
        });
    },
    greenToken:greenToken,
    ajaxGet:function(obj){ //get请求
        if(obj){
            obj.params = (obj.params ? obj.params : {});
            obj.url=obj.url? `${request.url}${obj.url}` : '' ;
        }
        let user1=sessionStorage.getItem('user'); //获取greenToken
        if(!user1){ //判断token是否过期如果token过期则返回登录页面重新登录
            router.push({path:'/login'})
            return
        }
        user1= JSON.parse(user1)
        request.greenToken=user1.greenToken
        greenToken=user1.greenToken  //将token重新赋值   防止后面的的POST请求用老的token
        axios.get(obj.url, {
          params: obj.params,
            headers: {
                'greenToken': user1.greenToken
            }
        })
        .then(function(res) {
          if (res.data.code == 200) {
              let data=res.data.data
              obj.fn(data)
          } else {
            console.log(res)
            obj.that.$message({
              message: res.data.msg,
              type: "warning"
            });
          }
        })
        .catch(function(res) {
            console.log(res)
            obj.that.$message({
              message: "登录超时",
              type: "warning"
            });
            // router.push({path:'/login'})
        });
    },
    ajaxPost:function(obj){ //post请求
        if(obj){
            if(obj.header=='f'){
                obj.params = (obj.params ?  qs.stringify(obj.params) : {});
            }else{
                obj.params = (obj.params ? obj.params : {});
            }
            obj.url=obj.url? `${request.url}${obj.url}` : '' ; 
            obj.header=(obj.header=='f' ? this.headerFrom : this.headerJson)
            obj.type=obj.type? obj.type :'POST'
        }
        obj.header.greenToken=greenToken //重新获取greenToken   防止greenToken过期
        axios({
            url: obj.url,
            method: obj.type,
            data:obj.params,
            headers:obj.header
        })
        .then(res => {
            var data = res.data
            if (data.code == 200) {
                obj.fn(data)
            } else {
                obj.that.$message({
                    message: res.data.msg,
                    type: "warning"
                });
            }
        })
        .catch(err => {
            console.log(err)
            obj.that.$message({
              message: "请求出错",
              type: "warning"
            });
        })
    },
    ajaxDel:function(obj){  //删除请求
        if(obj){
            obj.params = (obj.params ? obj.params : {});
            obj.url=obj.url? `${request.url}${obj.url}` : '' ;
        }
        axios.delete(obj.url, {data: obj.params})
        .then(function(res) {
            let data=res.data
            if(data.code==200){
                obj.fn(data)
            }else{
                obj.that.$message({
                    message: res.data.msg,
                    type: "warning"
                });
            }
            
        })
        .catch(err => {
            console.log(err)
            obj.that.$message({
              message: "请求出错",
              type: "warning"
            });
        })
    },
    dowload:function(obj){
        if(obj){
            obj.params = (obj.params ? obj.params : {});
            obj.url=obj.url? `${request.url}${obj.url}` : '' ;
        }
        let user1=sessionStorage.getItem('user'); //获取greenToken
        if(!user1){ //判断token是否过期如果token过期则返回登录页面重新登录
            router.push({path:'/login'})
            return
        }
        user1= JSON.parse(user1)
        request.greenToken=user1.greenToken
        greenToken=user1.greenToken  //将token重新赋值   防止后面的的POST请求用老的token
        axios.get(obj.url, {
            params: obj.params,
            headers: {
                'greenToken': user1.greenToken
            },
            responseType: 'arraybuffer'
        })
        .then(res => {
            var blob = new Blob([res.data], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'});
            var href = URL.createObjectURL(blob); //创建下载的链接
            window.location.href=href;
        })
        .catch(error => {
            console.log(error);
        });
    }
}
export { request ,url};